<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
	<head>
	    <base href="<%=basePath%>">
		<title>兑换商品</title>
        <%@include file="../collect/meta.jsp"%>
		<link rel="stylesheet" href="css/radius.css" type="text/css"></link>
		<script type="text/javascript" src="${ctx}/js/returnTop.js"></script>
	</head>
	<body lang="zh-cn">
	<div class="wrapper">	
       <c:import url="/muser/musertop.jsp"></c:import>
		<div class="main-container" id="main-container">
			<div class="main-container-inner">
				<c:import url="/muser/muserleft.jsp"></c:import>
				<div class="main-content">
					<div class="breadcrumbs" id="breadcrumbs">
						<ul class="breadcrumb">
							<li><i class="icon-home home-icon"></i>兑换商品</li>
						</ul>
					</div>
					<div class="page-content">
						<div class="row">
							<div class="col-xs-12">
								 <form action="member!seekMember" name="seekMember" method="post">
						         	 <fieldset>
						         	 		<legend align="center">兑换搜索</legend>
									 	 	<div align="center">
									 	 		<input type="text"  id="mobile" name="member.mobile" placeholder="手机号" maxlength="11" />
									 	 		<input type="hidden" name="seekType" value="exchange"/>
									 	 		<input type="button" class="btn btn-primary radius btn-sm" id="seek" value="搜&nbsp;索"/>
									 	 		<%if(request.getParameter("error")!=null) {%>
						         	 					&nbsp;&nbsp;&nbsp;<i style="color: red">该手机号还未注册会员</i>
						         	 			<%} %>
						         	 			<c:if test="${message==''}">
						         	 			</c:if>
						         	 			<c:if test="${message!=''}">
						         	 			<lable style="color: red">${message }</lable>
						         	 			</c:if>
									 	 	</div>
									 </fieldset>
								 </form><br/>
								 <%if(request.getParameter("temp")!=null){ %>
								 <form action="member!exchange" name="exchange" method="post">
								 	<fieldset>
								 		<legend align="center">会员信息</legend>
								 		<div align="center">
								 		  <table border="0" cellspacing="1" cellpadding="3">
			                                <tr>
			                                  <td><div align="right">手&nbsp;机&nbsp;号:</div></td>
			                                  <td>
			                                  	<input type="hidden"  name="member.mobile" value="${member.mobile }" id="memberMobile" />${member.mobile }
			                                  </td>
			                                </tr>
			                                <tr>
			                                  	<td><div align="right">姓&nbsp;&nbsp;&nbsp;&nbsp;名:</div></td>
			                                  	<td>
			                                  		<c:if test="${not empty member.name }">
			                                  			${member.name }
			                                  		</c:if>
			                                  	</td>
			                                </tr>
			                                <tr>
			                                  	<td><div align="right">性&nbsp;&nbsp;&nbsp;&nbsp;别:</div></td>
			                                  	<td>
			                                  		<c:if test="${not empty member.sex }">
			                                  			${member.sex }
			                                  		</c:if>
			                                  	</td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right">注册时间:</div></td>
			                                  <td>
			                                  	${fn:substring(member.createTime, 0, fn:indexOf(member.createTime, "."))}</td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right">会员积分:</div></td>
			                                  <td>
			                                  	<input type="hidden" id="score"
													name="baseMember.score" value="${baseMember.score}" />
													${fn:substring(baseMember.score, 0, fn:indexOf(baseMember.score, ".0"))}
			                                  </td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right">会员等级:</div></td>
			                                  <td>${baseMember.level}</td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right" >会员余额:</div></td>
			                                  <td><fmt:formatNumber value="${baseMember.balance}" pattern="#0.00"/>
			                                  </td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right">兑换产品:</div></td>
			                                  <td>
			                                     <select id="exGoodid" name="exGoodid" style="width: 130px;">
													<c:forEach items="${exGoods }" var="exchangeGood">
														<option value="${exchangeGood.id }">${exchangeGood.goodname }</option>
													</c:forEach>
												 </select>
											  </td>
			                                </tr>
			                                <tr>
			                                  <td><div align="right">需要积分:</div></td>
			                                  <td>
			                                  <input type="hidden"  id="exScore" name="exScore" value="${exGoods[0].score}"/>
			                                  <div id="needscore">${fn:substring(exGoods[0].score, 0, fn:indexOf(exGoods[0].score, '.0')) }</div>
			                                  	<c:forEach items="${exGoods }" var="exchangeGood">
													<input type="hidden" id="${exchangeGood.id }" value="${fn:substring(exchangeGood.score, 0, fn:indexOf(exchangeGood.score, '.0')) }">
												</c:forEach>
			                                  </td>
			                                </tr>
			                                <tr>
			                                  <td colspan="2"><div align="center"><input type="button" class="btn btn-primary radius" id="but" value="兑换" /></div></td>
			                                </tr>
			                                <tr>
			                                  <td colspan="2">
			                                  	<div align="center">
			                                     	<b id="backMessage" style="font-size:25px;color:red;">
			                                     		<c:choose>
			                                     			<c:when test="${back == 1 }">兑换成功！</c:when>
			                                     			<c:when test="${back == 2 }">库存不足，不能兑换</c:when>
			                                     			<c:when test="${back == 3 }">没有可兑换商品</c:when>
			                                     			<c:when test="${back == 4 }">积分不足，不能兑换</c:when>
			                                     			<c:when test="${back == 5 }">该手机号未注册</c:when>
			                                     		</c:choose>
			                                     	</b>
			                                     </div>
			                                     <input type="hidden" name="back" id="back" value="${back }"/>
			                                  </td>
			                                </tr>
			                                <input type="hidden"  name="member.eaId" value="${member.eaId }" />
			                                <input type="hidden"  name="member.id"  value="${member.id }" />
			                                <input type="hidden"  name="baseMember.id"  value="${baseMember.id }"/>
			                                <input type="hidden"  name="shop.id"  value="${shop.id }" />
			                              </table>
								 		</div>
								 		<legend align="center">兑换记录</legend>
										<table class="table table-striped table-bordered table-hover">
											<tr>
												<th><div align="center">兑换商品</div></th>
												<th><div align="center">消耗积分</div></th>
												<th><div align="center">操作人</div></th>
												<th><div align="center">兑换店铺</div></th>
												<th><div align="center">兑换时间</div></th>
											</tr>
										<c:forEach items="${exchangeLogs}" var="exchangeLog" varStatus="status">
										  <c:if test="${status.count<=10}">
								            <tr>
								                <td><div align="center">${exchangeLog.exchangeGood.goodname }</div></td>
								                <td><div align="center">${exchangeLog.exchangeGood.score}</div></td>
								                 <td><div align="center">${exchangeLog.userName}</div></td>
								                <td><div align="center">${exchangeLog.shop.name}</div></td>
								                <td><div align="center">${exchangeLog.createTime }</div></td>
								            </tr>
								          </c:if>
							            </c:forEach>
										</table>
								 	</fieldset>
								 </form>
								 <%} %>	
							</div><!-- /.col -->
						</div><!-- /.row -->
					</div><!-- /.page-content -->
				</div><!-- /.main-content -->
			</div><!-- /.main-container-inner -->
			<a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse" style="position:fixed;bottom:0;">
				<i class="icon-double-angle-up icon-only bigger-110"></i>
			</a>
		</div><!-- /.main-container -->
		<div class="push"></div>
     </div>
     <c:import url="/collect/down.jsp"></c:import>
     
	 <script type="text/javascript">
			$(document).ready(function(){
				
			    var back = $("#back").val();
                if (back == "" || back == null) {
			       $("#backMessage").hide();
		        }else{
		           setTimeout("$('#backMessage').hide()",5000);
		        }
				
				$("#seek").click(function(){
						var mobile = $("#mobile").val();
						if(mobile.length==0){
							alert("请填写手机号");
							return false;
					 	}
					 	if(!(/^1[3|5|8][0-9]\d{4,8}$/.test(mobile))||mobile.length!=11){
					 		alert("手机格式有误");
					 		return false;
					    }
					    seekMember.submit();
				});
				
				$("#exGoodid").change(function(){
                   var exGoodvalue = $("#exGoodid").val();
                   if (exGoodvalue == "" || exGoodvalue == null) {
			           alert("无商品可提供兑换！");
			           return false;
		            }
		           var exGoodScore= $("#"+exGoodvalue).val();
                   $("#exScore").val(exGoodScore);
                   $("#needscore").html(exGoodScore);
                });
				
				$("#but").click(function(){
					
					if (!confirm("确认兑换？")) {
						return false;
					}
					
				    var userscore = $("#score").val();
				    var exScore = $("#exScore").val();
					var goodvalue = $("#exGoodid").val();
					if (goodvalue == "" || goodvalue == null) {
			           alert("无商品可提供兑换！");
			           return false;
		            }
		            if ((userscore - exScore) < 0) {
			           alert("用户积分不足！");
			           return false;
		            }
					var memberMobile = $("#memberMobile").val();
					
					$("#but").attr("disabled", true);
					
					$.ajax( {
						url : "member!timeOutExchageLog",
						type : "post",
						data : {
							"memberMobile" : memberMobile.toString()
						},
						datatype : "json",
						success : function(data) {
							var d = eval("(" + data + ")");
							var b = d.timeOut;
							if (b) {
								$("#but").attr("disabled", false);
								alert("不能在30秒内重复兑换同一个账号");
							} else {
								// 检查会员兑换同一件商品的次数是否到达上限
								$.ajax( {
									url : "exchangegood!checkExchangeAmount",
									type : "post",
									data : {
										"memberId" : $("input[name='member.id']").val(),
										"exgoodId" : $("#exGoodid").val()
									},
									datatype : "json",
									success : function(data) {
										var d = eval("(" + data + ")");
										var b = d.temp;
										if (b == 'error') {
											$("#but").attr("disabled", false);
											alert("此商品限定每个会员可以兑换" + d.count + "次，该会员已经不能再兑换此商品！");
										} else if (b == 'ok') {
											$("form[name='exchange']").submit();
										} else {
											$("form[name='exchange']").submit();
										}
									},
									error : function(request) {
										$("#but").attr("disabled", false);
										alert("Connection error");
									}
								});
								
							}
						},
						error : function(request) {
							$("#but").attr("disabled", false);
							alert("Connection error");
						}
					});
					
				 });
			});
	  </script>
	 <script type="text/javascript">
	  	$(document).ready(function(){
			$('.duihuan > ul').show();
			$('.duihuan > ul > li > a').eq(0).css("color","red");
	  	});
	 </script>
</body>
</html>
